package com.test.flink_sinks;

import com.alibaba.fastjson.JSON;
import com.test.beans.UserBean;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.MemorySize;
import org.apache.flink.connector.file.sink.FileSink;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.OutputFileConfig;
import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.DateTimeBucketAssigner;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.time.Duration;

public class F02SinkFile {

    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port", 8888);
        StreamExecutionEnvironment webUI = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(configuration);
        webUI.enableCheckpointing(2000, CheckpointingMode.AT_LEAST_ONCE);

        DataStreamSource<String> data = webUI.socketTextStream("127.0.0.1", 9001);

        // 数据转换成bean
        DataStream<UserBean> ds =  data.map((MapFunction<String, UserBean>) line -> {
            // 数据在解析的时候可能会抛出异常 需要捕获异常防止程序停止  trycatch 快捷键 ctr+alt+t
            UserBean userBean = new UserBean();
            try {
                String[] arr = line.split(",");
                userBean.setUid(Integer.parseInt(arr[0]));
                userBean.setName(String.valueOf(arr[1]));
                userBean.setAge(Integer.parseInt(arr[2]));
                userBean.setAddress(String.valueOf(arr[3]));
                userBean.setBirthday(Long.parseLong(arr[4]));
            } catch (Exception e) {

            }
            return userBean;
        });

        // bean转成字符串
        DataStream<String> dss = ds.map(JSON::toJSONString);

        Path path = new Path("data/sink/");
        SimpleStringEncoder<String> simpleStringEncoder = new SimpleStringEncoder<>();

        FileSink<String> sink = FileSink.forRowFormat(path, simpleStringEncoder)
                //分桶规则 [分文件夹管理数据 数据输出到指定规则的文件夹中]
                .withBucketAssigner(new DateTimeBucketAssigner())
                //输出文件的滚动策略
                .withRollingPolicy(DefaultRollingPolicy.builder()
//                        .withInactivityInterval(3)
                        .withMaxPartSize(MemorySize.ofMebiBytes(10))
//                        .withRolloverInterval(Duration.ofSeconds(10))
                        .build())
                .withBucketCheckInterval(100)
                .withOutputFileConfig(OutputFileConfig.builder()
                        .withPartPrefix("dow-")
                        .withPartSuffix(".txt")
                        .build())
                .build();

        dss.sinkTo(sink);

        webUI.execute();
    }
}
